<?php

namespace app\index\service\wechatprocess;
use app\common\model\jsm\Ibeacon as IbeaconModel;
use app\common\model\oms\Store as StoreModel;
use app\common\model\log\InspectionLog as InspectionLogModel;

/**
 * 微信扫码service
 */
class WechatShakeProcessorService extends BaseProcessorService{

    private $_openid  = null;
    private $_appid   = null;
    private $_companyId = null;
    private $_mpId    = null;
    private $_xmlArr  = null;
    private $_message = null;
    
    
    /**
     * 构造函数
     * @param type $mps
     * @param type $message
     */
    public function __construct($mps,$message) {
        
        $this->_message = $message;
        $this->_xmlArr = $message['XmlArr'];
        
        $this->_openid = $message['openid'];
        $this->_appid = $message['app_id'];
        $this->_companyId = $mps['o_id'];
        $this->_mpId = $mps['mp_id'];
    }
    
    
    /**
     * 处理摇一摇接口操作
     * @return boolean
     */
    public function DealShake(){
        
        $message = $this->_message;
        //不处理非宝岛的业务
        if($this->_appid != self::BAODAO_APP_ID){
            return ['msg'=>'appId is no baodao'];
        }
        //将key值全部小写
        $this->_xmlArr = array_change_key_case($this->_xmlArr);
        //获取当前ibeacon信息
        $ChosenBeacon   = $this->_xmlArr['chosenbeacon'];
        $ChosenBeacon   = array_change_key_case((array)$ChosenBeacon);
        $major    = $ChosenBeacon['major'];
        $minor    = $ChosenBeacon['minor'];
        $uuid     = $ChosenBeacon['uuid'];
        $add_time = $message['add_time'];
        
        $ibeaconModel = new IbeaconModel();
        $IbeaconInfo = $ibeaconModel->getIbeaconByCondition(['major'=>$major,'minor'=>$minor,'uuid'=>$uuid]);
        $store_id     = isset($IbeaconInfo['bind_store_id'])?$IbeaconInfo['bind_store_id']:0;
        
        if(empty($IbeaconInfo)){
            //没有当前设备的话，把设备记录下来
            $result = $this->_saveIbeacon($uuid, $major, $minor, $add_time);
            if( $result !== true){
                return $result;
            }
        }
        
        //巡店信息数组拼接(其实可以用数组$data+$ChosenBeacon+ts即可（可惜swoole不支持冗余插入过滤）)
        $InspectionData = [];
        $InspectionData['store_id'] = $store_id;
        $InspectionData['distance'] = $ChosenBeacon['distance'];
        $InspectionData['major']    = $major;
        $InspectionData['minor']    = $minor;
        $InspectionData['uuid']     = $uuid;
        $InspectionData['xml']      = $this->_message['xml'];
        $InspectionData['open_id']  = $this->_openid;
        $InspectionData['mp_id']    = $this->_mpId;
        $InspectionData['ts']       = strtotime(date('Y-m-d H:i:00',$message['add_time']));
        
        //添加城市信息
        $InspectionData = $this->_addCityInfo($InspectionData,$store_id);
        $InspectionLogModel = new InspectionLogModel();
        try{
            $res = $InspectionLogModel->addInspectionLog($InspectionData);
            if($res === false){
                return  ['msg'=>'Inspection_log table insert error'];
            }
            return true;
            
        } catch (\think\Exception $ex){
            return  ['msg'=>'Inspection_log table insert time is less than 1 minute'];
        }
    }
    
    /**
     * 保存ibeacon信息
     */
    private function _saveIbeacon($uuid,$major,$minor,$add_time){
        
        $ibeaconModel = new IbeaconModel();
        
        $IbeaconData = [];
        $IbeaconData['mp_id']     = $this->_mpId;
        $IbeaconData['name']      = '未设置';
        $IbeaconData['device_id'] = '0000000';
        $IbeaconData['uuid']      = $uuid;
        $IbeaconData['major']     = $major;
        $IbeaconData['minor']     = $minor;
        $IbeaconData['poi_id']    = 0;
        $IbeaconData['comment']   = '摇一摇巡店功能设备';
        $IbeaconData['status']    = 1;
        $IbeaconData['serial_number']   = 0;
        $IbeaconData['last_update_ts']  = $add_time;
        $IbeaconData['return_msg']      = 'Device is null,add this record by shakearUserShakeProcessor';
        $IbeaconData['company_id']      = 0;
        $IbeaconData['company_code']    = '';
        $IbeaconData['company_name']    = '';
        $IbeaconData['bind_store_id']   = 0;
        $IbeaconData['bind_store_name'] = '';
        $IbeaconData['reffer_page_number'] = 0;
        $IbeaconData['is_delete']          = 0;
        $IbeaconData['last_yaoyiyao_ts'] = $add_time;
        $IbeaconData['copy_storeid_ts']  = $add_time;

        $result = $ibeaconModel->insertIbeacon($IbeaconData);

        if($result === false){
            return ['msg'=>'insert ibeacon is error'];
        }
        return true;
    }
    
    /**
     * 给inspectionArr加上城市信息
     * @param type $InspectionArr
     * @param type $store_id
     */
    private function _addCityInfo($InspectionArr,$store_id){
        if(!$store_id){
            $InspectionArr['region_id'] = 0;
        }
        
        //去查询门店信息
        $storesModel = new StoreModel();
        $store = $storesModel->getStoreByCondition(['store_id'=>$store_id]);
        
        if($store){
            $InspectionArr['region_id'] = $store['region_city_id'];
        }else{
             $InspectionArr['region_id'] = 0;
        }
        
        return $InspectionArr;
    }
}
